﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InsuranceSell.Code.Entities;
using InsuranceSell.Code.Services;
using InsuranceSell.Code.Data;
using System.Globalization;

namespace InsuranceSell
{
    public partial class frmMain : Form
    {
        public string strFullName = string.Empty;
        public bool isAdmin = false;
        public frmMain()
        {
            InitializeComponent();
        }

        private void imbsearch_Click(object sender, EventArgs e)
        {
            FindCustomer findCustomer = new FindCustomer();
            findCustomer.date = dtpSearch.Value;
            if (chkInday.Checked == true)
            {
                findCustomer.between = 0;
            }
            else {
                if (chkInmonth.Checked == true)
                {
                    findCustomer.between = 30;
                }
                else {
                    findCustomer.between = 7;
                }
            }
            DataTable result = FindCustomerServices.SearchCustomer(findCustomer);
            if (result != null) {
                dtgResult.DataSource = result;
                result.Dispose();
            }
        }

        private void mniNew_Click(object sender, EventArgs e)
        {
            frmAddNew AddNew = new frmAddNew();
            AddNew.Show();
        }

        private void mAccountManager_Click(object sender, EventArgs e)
        {
            frmAccountManager AC = new frmAccountManager();
            AC.Show();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            //System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("vi");
            //System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
            //CultureInfo culture = CultureInfo.CreateSpecificCulture("vi");
            //Application.CurrentCulture = culture;

            dtpSearch.Format = DateTimePickerFormat.Custom;
            //string[] format = dtpSearch.Value.GetDateTimeFormats(Application.CurrentCulture);
            dtpSearch.CustomFormat = "dd-MM-yyyy";

            //System.Globalization.DateTimeFormatInfo.CurrentInfo.Calendar = culture.Calendar;

            mAccountManager.Visible = isAdmin;
            if (strFullName != string.Empty)
            {
                mLogout.Text = strFullName + "[Thoát]";
            }
            else {
                mLogout.Text = "[Đăng Nhập]";
            }

            DataTable allCustomers = CustomerServices.LoadAllCustomer();

            if (allCustomers != null)
            {
                dtgResult.DataSource = allCustomers;
                allCustomers.Dispose();
            }
        }

        private void dtgResult_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                frmAddNew frmEdit = new frmAddNew();
                frmEdit.txtAccountBank.Text = dtgResult.Rows[e.RowIndex].Cells[12].Value.ToString();
                frmEdit.txtAddress.Text = dtgResult.Rows[e.RowIndex].Cells[6].Value.ToString();
                frmEdit.txtBankName.Text = dtgResult.Rows[e.RowIndex].Cells[11].Value.ToString();
                frmEdit.txtContractNo.Text = dtgResult.Rows[e.RowIndex].Cells[1].Value.ToString();
                frmEdit.txtCustomerName.Text = dtgResult.Rows[e.RowIndex].Cells[0].Value.ToString();
                frmEdit.txtEmail.Text = dtgResult.Rows[e.RowIndex].Cells[9].Value.ToString();
                frmEdit.txtInsuranceTarget.Text = dtgResult.Rows[e.RowIndex].Cells[3].Value.ToString();
                frmEdit.txtMobile.Text = dtgResult.Rows[e.RowIndex].Cells[7].Value.ToString();
                frmEdit.txtTelephone.Text = dtgResult.Rows[e.RowIndex].Cells[8].Value.ToString();
                frmEdit.txtPresentativeJobTitle.Text = dtgResult.Rows[e.RowIndex].Cells[14].Value.ToString();
                frmEdit.txtPresentativeName.Text = dtgResult.Rows[e.RowIndex].Cells[13].Value.ToString();
                frmEdit.txtTaxCode.Text = dtgResult.Rows[e.RowIndex].Cells[10].Value.ToString();
                frmEdit.dtpStartDate.Value = DateTime.Parse(dtgResult.Rows[e.RowIndex].Cells[4].Value.ToString());
                frmEdit.dtpEndDate.Value = DateTime.Parse(dtgResult.Rows[e.RowIndex].Cells[5].Value.ToString());
                frmEdit.cbbIsuranceType.SelectedItem = dtgResult.Rows[e.RowIndex].Cells[2].Value.ToString();
                frmEdit.ShowDialog();
                DataTable allCustomers = CustomerServices.LoadAllCustomer();

                if (allCustomers != null)
                {
                    dtgResult.DataSource = allCustomers;
                    allCustomers.Dispose();
                }
            }
        }


        private void menuLogin_Click(object sender, EventArgs e)
        {
            frmLogin loginForm = new frmLogin();
            loginForm.Show();
        }

        private void mLogout_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
